package Dao;

import Bean.User;
import Utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 实现用账号密码查询用户
 */
public class UserDao {
    public User getUser(String userName,String passWord) {
        User user = User.getInstance();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "select * from tb_users where userName = ? && passWord = ?";
            //执行sql
            pst = con.prepareStatement(sql);
            //设置参数
            pst.setString(1,userName);
            pst.setString(2,passWord);
            //接收结果
            rs = pst.executeQuery();
            while (rs.next()){
                user.setId(rs.getInt(1));
                user.setUserName(rs.getString(2));
                user.setPassWord(rs.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        //返回用户
        return user;
    }
}
